package
{
	import avmplus.getQualifiedClassName;
	
	import com.pickgliss.keyboard.KeyboardManager;
	import com.pickgliss.loaders.ILoader;
	import com.pickgliss.loaders.LoaderManager;
	import com.pickgliss.toplevel.StageReferance;
	import com.pickgliss.utils.StringUtils;
	
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	
	import mx.core.UIComponent;
	
	public class PreviewContent extends UIComponent
	{
		private var _imageContainer:Sprite;
		private var _mask:Shape;
		
		[Bindable]
		public var sourceBitmapData:BitmapData;
		
		public function PreviewContent()
		{
			super();
		}
		
		public function set source(url:String):void
		{
			if(StringUtils.getFileExtendtionFromUrl(url) == "png")
			{
				LoaderManager.Instance.loadEasy(url,onloadSourceComplete);
			}
		}
		
		private function onloadSourceComplete(loader:ILoader):void
		{
			sourceBitmapData = loader.content["bitmapData"];
			_imageContainer.removeChildren();
			_imageContainer.addChildAt(loader.content,0);
		}
		
		override protected function createChildren():void
		{
			_imageContainer = new Sprite();
			addChild(_imageContainer);
			_mask = new Shape();
			addChild(_mask);
			_imageContainer.mask = _mask;
		}
		
		override protected function childrenCreated():void
		{
			addEventListener(Event.ADDED_TO_STAGE,addTostage);
			super.childrenCreated();
		}
		
		private function addTostage(event:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE,addTostage);
			addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown,false,0,true);
			KeyboardManager.Instance.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown,false,0,true);
			KeyboardManager.Instance.addEventListener(KeyboardEvent.KEY_UP,onKeyup,false,0,true);
		}
		
		private function onKeyDown(event:KeyboardEvent):void
		{
			if(event.keyCode == Keyboard.SPACE)
			{
				Mouse.cursor = PngFileExtender.MOVE_CURSOR;
			}
		}
		
		private function onKeyup(event:KeyboardEvent):void
		{
			if(event.keyCode == Keyboard.SPACE)
			{
				Mouse.cursor = MouseCursor.AUTO;
			}
		}
		
		private function onMouseDown(event:MouseEvent):void
		{
			if(KeyboardManager.Instance.keyIsDown(Keyboard.SPACE))
			{
				_imageContainer.startDrag(false);
			}
			
			StageReferance.stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp,false,0,true);
		}
		
		private function onMouseUp(event:MouseEvent):void
		{
			StageReferance.stage.removeEventListener(MouseEvent.MOUSE_UP,onMouseUp);
			_imageContainer.stopDrag();
		}
		
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			updateMask(unscaledWidth,unscaledHeight);
			super.updateDisplayList(unscaledWidth,unscaledHeight);
		}
		
		private function updateMask(width:Number,height:Number):void
		{
			_mask.graphics.clear();
			_mask.graphics.beginFill(0xff0000,0);
			_mask.graphics.drawRect(0,0,width,height);
			_mask.graphics.endFill();
		}
	}
	
	
}